package br.sed.theenemy.presentation.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import br.sed.theenemy.business.exception.DBException;
import br.sed.theenemy.business.exception.InvalidActionException;
import br.sed.theenemy.business.exception.InvalidUniqueBeanException;
import br.sed.theenemy.business.exception.InvalidUserException;
import br.sed.theenemy.business.services.Services;
import br.sed.theenemy.business.utils.Utils;
import br.sed.theenemy.model.bean.Action;
import br.sed.theenemy.model.bean.User;
import br.sed.theenemy.presentation.EnemyConstants;
import br.sed.theenemy.presentation.UserSession;
import br.sed.theenemy.presentation.form.ActionsForm;
import br.sed.theenemy.presentation.vo.ActionVO;
import br.sed.theenemy.presentation.vo.UserVO;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class ActionAction extends DispatchAction {

	// forwards
    private final String ACTION_PAGE = "ActionPage";
    private final String LOGIN_PAGE = "LoginPage";
    private final String ACTION_PROFILE_PAGE = "ActionProfilePage";
        
	public ActionForward unspecified(final ActionMapping mapping,
	           final ActionForm form, final HttpServletRequest request, 
	           final HttpServletResponse response) {
		ActionsForm actionsForm = (ActionsForm) form;
		Services services = Services.getInstance();
		
		final UserSession session = new UserSession(request, false);
        final User userLogged = session.getLoggedUser();
        
        //Filling user Data
        if (userLogged == null) {
            // User not logged in, redirect to login.jsp
            return mapping.findForward(LOGIN_PAGE);
        }
        
        Long userId = actionsForm.getId();
        User user = null;
        if(!(userId == null || userId <= 0)){
        	try {
				user = services.findUserById(userId);
			} catch (InvalidUniqueBeanException e) {
			    return mapping.findForward(EnemyConstants.ERROR_PAGE);
			} catch (DBException e) {
			    return mapping.findForward(EnemyConstants.ERROR_PAGE);
			} catch (InvalidUserException e) {
			    return mapping.findForward(EnemyConstants.ERROR_PAGE);
            }
        }else{
        	user = userLogged;
        }
        
        UserVO userVO = new UserVO(user);
        actionsForm.setUser(userVO);
        
        //setting user comment
        Action actionComment = null;
		try {
			actionComment = services.findLastCommentByUserId(user.getId());
		} catch (DBException e) {
		    return mapping.findForward(EnemyConstants.ERROR_PAGE);
		}
		if(actionComment!=null){
			actionsForm.setComment(actionComment.getComment());
		}else{
			actionsForm.setComment("Sem comentário");
		}
        
        //find actions relacionadas ao user
        List<ActionVO> actionListVO = null;
        try {
            actionListVO = services.findActionStream(user.getId());
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUniqueBeanException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUserException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        
        actionsForm.setActionList(actionListVO);
        
        return mapping.findForward(ACTION_PROFILE_PAGE);
	}
	
	public ActionForward home(final ActionMapping mapping,
                final ActionForm form, final HttpServletRequest request, 
                final HttpServletResponse response) {
         ActionsForm actionsForm = (ActionsForm) form;
         Services services = Services.getInstance();
         
         final UserSession session = new UserSession(request, false);
         final User userLogged = session.getLoggedUser();
         
         if (userLogged == null) {
             // User not logged in, redirect to login.jsp
             return mapping.findForward(LOGIN_PAGE);
         }
         
         //Filling user Data
         UserVO userVO = new UserVO(userLogged);
         actionsForm.setUser(userVO);
         
         //setting user comment
         Action actionComment = null;
         try {
             actionComment = services.findLastCommentByUserId(userLogged.getId());
         } catch (DBException e) {
             return mapping.findForward(EnemyConstants.ERROR_PAGE);
         }
         if(actionComment!=null){
             actionsForm.setComment(actionComment.getComment());
         }else{
             actionsForm.setComment("Sem comentário");
         }
         
         //find actions relacionadas ao user
         List<ActionVO> actionListVO = null;
         try {
             actionListVO = services.findAllActionStream(userLogged.getId());
         } catch (DBException e) {
             return mapping.findForward(EnemyConstants.ERROR_PAGE);
         } catch (InvalidUniqueBeanException e) {
             return mapping.findForward(EnemyConstants.ERROR_PAGE);
         } catch (InvalidUserException e) {
             return mapping.findForward(EnemyConstants.ERROR_PAGE);
         }
         
         actionsForm.setActionList(actionListVO);
         
         return mapping.findForward(ACTION_PAGE);
     }
	
	public ActionForward comment(final ActionMapping mapping,
	           final ActionForm form, final HttpServletRequest request, 
	           final HttpServletResponse response) {
		ActionsForm actionsForm = (ActionsForm) form;
		Services services = Services.getInstance();
		
		final UserSession session = new UserSession(request, false);
        final User userLogged = session.getLoggedUser();
        
        //Filling user Data
        if (userLogged == null) {
            // User not logged in, redirect to login.jsp
            return mapping.findForward(LOGIN_PAGE);
        }
        
        if (!form.validate(mapping, request).isEmpty()) {
          return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        
        String comment = actionsForm.getComment();
        
        if(Utils.isNullOrEmpty(comment)){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        
		try {
            services.createActionComment(userLogged.getId(), comment);
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }

		return null;
	}
	
	public ActionForward attack(final ActionMapping mapping,
	           final ActionForm form, final HttpServletRequest request, 
	           final HttpServletResponse response) {
		ActionsForm actionsForm = (ActionsForm) form;
		Services services = Services.getInstance();
		
		final UserSession session = new UserSession(request, false);
		final User userLogged = session.getLoggedUser();
		
		//Filling user Data
		if (userLogged == null) {
		    // User not logged in, redirect to login.jsp
		    return mapping.findForward(LOGIN_PAGE);
		}
	     
		if (!form.validate(mapping, request).isEmpty()) {
	          return mapping.findForward(EnemyConstants.ERROR_PAGE);
	    }
		
	     String comment = actionsForm.getComment();
	     Long actionIdRel = actionsForm.getActionIdRel();
     
        if(Utils.isNullOrEmpty(comment)){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        if(actionIdRel<=0){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
	     
     	try {
            services.createActionAttack(userLogged.getId(), actionIdRel, comment);
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUniqueBeanException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUserException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidActionException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }

		return null;
	}
	
	public ActionForward defend(final ActionMapping mapping,
	           final ActionForm form, final HttpServletRequest request, 
	           final HttpServletResponse response) {
		ActionsForm actionsForm = (ActionsForm) form;
		Services services = Services.getInstance();
		
		final UserSession session = new UserSession(request, false);
		final User userLogged = session.getLoggedUser();
		
		//Filling user Data
		if (userLogged == null) {
			// User not logged in, redirect to login.jsp
			return mapping.findForward(LOGIN_PAGE);
		}
		  
		String comment = actionsForm.getComment();
		Long actionIdRel = actionsForm.getActionIdRel();
  
		if(Utils.isNullOrEmpty(comment)){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
        if(actionIdRel<=0){
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }
		
		try {
            services.createActionDefend(userLogged.getId(), actionIdRel, comment);
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUniqueBeanException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUserException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidActionException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }

		return null;
	}
	
	public ActionForward support(final ActionMapping mapping,
	           final ActionForm form, final HttpServletRequest request, 
	           final HttpServletResponse response) {
		ActionsForm actionsForm = (ActionsForm) form;
		Services services = Services.getInstance();
		
		final UserSession session = new UserSession(request, false);
		final User userLogged = session.getLoggedUser();
		
		//Filling user Data
		if (userLogged == null) {
			// User not logged in, redirect to login.jsp
			return mapping.findForward(LOGIN_PAGE);
		}
		  
		String comment = actionsForm.getComment();
		Long actionIdRel = actionsForm.getActionIdRel();

		if(Utils.isNullOrEmpty(comment)){
		    return mapping.findForward(EnemyConstants.ERROR_PAGE);
		}
		if(actionIdRel<=0){
		    return mapping.findForward(EnemyConstants.ERROR_PAGE);
		}
		
		try {
            services.createActionSupport(userLogged.getId(), actionIdRel, comment);
        } catch (DBException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUniqueBeanException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidUserException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        } catch (InvalidActionException e) {
            return mapping.findForward(EnemyConstants.ERROR_PAGE);
        }

		return null;
	}
	
	public ActionForward attackUser(final ActionMapping mapping,
            final ActionForm form, final HttpServletRequest request, 
            final HttpServletResponse response) {
     ActionsForm actionsForm = (ActionsForm) form;
     Services services = Services.getInstance();
     
     final UserSession session = new UserSession(request, false);
     final User userLogged = session.getLoggedUser();
     
     //Filling user Data
     if (userLogged == null) {
         // User not logged in, redirect to login.jsp
         return mapping.findForward(LOGIN_PAGE);
     }
      
      String comment = actionsForm.getComment();
      Long userTargetId = actionsForm.getActionIdRel();
  
     if(Utils.isNullOrEmpty(comment)){
         return mapping.findForward(EnemyConstants.ERROR_PAGE);
     }
     if(userTargetId==null || userTargetId<=0){
         return mapping.findForward(EnemyConstants.ERROR_PAGE);
     }
      
     try {
        services.createActionAttackUser(userLogged.getId(), userTargetId, comment);
    } catch (DBException e) {
        return mapping.findForward(EnemyConstants.ERROR_PAGE);
    } catch (InvalidUniqueBeanException e) {
        return mapping.findForward(EnemyConstants.ERROR_PAGE);
    } catch (InvalidUserException e) {
        return mapping.findForward(EnemyConstants.ERROR_PAGE);
    }

     return null;
 }
}
